/**
 * Check Comment Body Before Sending
 */


$('.js_edit_header_bar').html('');

function trim(str, chars) {
        if(str == null || str == ''){
            return str;
        }
	return ltrim(rtrim(str, chars), chars);
}
function ltrim(str, chars) {
        if(str == null || str == ''){
            return str;
        }
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
function rtrim(str, chars) {
        if(str == null || str == ''){
            return str;
        }
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
var fwall = {
  minWidth : 45,
  minHeight : 45,
  read : function(a, b, c, d, l) {
    try {
      var o = new Image();
      o.onAbort = function() {
      }
      o.onError = function() {
      }
      o.onload = function() {
        var w = o.width, h = o.height, k = o.width * o.height;
        if (w >= b && h >= c) {
          d(k, o, a, w, h, l);
        }
      };
      o.src = a;
    } catch (e) {
    }
  },
  reads : function(a, b) {
    var o = this, s = '';
    for ( var i in a) {
      this.read(a[i], o.minWidth, o.minHeight, o.thumb.add, o.getLink);
    }
  },
  mat : function(o, call) {
    o['core[ajax]'] = true;
    o['core[call]'] = call;
    o['core[is_admincp]'] = 0;
    o['core[security_token]'] = oCore['log.security_token'];
    return o;
  },
  ajaxUrl : function() {
    return (oParams.sJsStatic + 'ajax.php');
  },
  touch: function (a, b, c ){
     $('div#js_item_feed_'+a+' .feed_touch').html(fwall.getIndImg());
     $.post(fwall.ajaxUrl(), fwall.mat( {
         'feed_id': a, 'wall_id': b, code: c
      },'wall.touch'),function(res){
        eval(res);
      });
  },
  title : function(a) {
    var c = '<span class="feed_title">' + a + '</span>';
    $('div#id_feed_result_title').html(c);
  },
  subtitle : function(a) {
    var e = this;
    $(e.getLink()).html(a).appendTo('div#id_feed_result_subtitle');
  },
  description : function(a) {
    var c = '<span class="feed_description">' + a + '</span>';
    $('div#id_feed_result_description').html(c);
  },
  itouch: function(a){
    $('div#js_item_feed_'+a+' .feed_touch').html('');
  },
  itouch_close: function(a){
    $('div#js_controller_feed_wrapper_'+a).html('');
  },
  clear : function() {
    $('#id_textarea_status').val('').height('16px').focus();
    $('input#id_hidden_parser').val('');
    $('div#id_div_status').html('');
    $('div#idfhd').html('');
    fwall.thumb.clear();
    fwall.form.clearAll();
    $('#id_feed_status').val('');
  },
  isLink : function(a) {
    var reg = new RegExp();
    reg.compile("[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
    return reg.test(a);
  },
  thumb : {
    k : -1,
    ci : -1,
    ii : [],
    il : -1,
    ss : true,
    next : function() {
      var e = this;
      if(e.ss){
        $(e.ii[e.il]).hide();
        if (++e.il >= e.ii.length) {
          e.il = 0;
        }
        $(e.ii[e.il]).show();
        e.ud();
      }
    },
    back : function() {
     var e = this;
      if(e.ss){
        $(e.ii[e.il]).hide();
        if (--e.il < 0) {
          e.il = e.ii.length - 1;
        }
        $(e.ii[e.il]).show();
        e.ud();
      }
    },
    add : function(k, o, a, w, h, l) {
      var e = fwall.thumb;
      if (k > e.k) {
        e.k = k;
        e.il = e.ii.length;
      }
      var c = $(l()).addClass('feed_thumb_link').css( {
        'display' : 'none'
      }).appendTo('div#id_feed_result_image');
      $(o).appendTo(c);
      e.ii.push(c);
    },
    clear : function() {
      var e = this;
      e.k = e.ci = e.il = -1;
      e.ss = true;
      e.ii = [];
    },
    show : function() {
      var e = this;
      e.ss = true;
      if (e.k > 0 && e.il > -1) {
        fwall.form.val('thumbnail', $(e.ii[e.il]).find('img').attr(
          'src'));
        $(e.ii[e.il]).show();
      }
      e.ud();
    },
    init : function() {
      var e = this;
      e.show();
      if (e.ii.length) {
        $('div#id_feed_thumb_manage').show();
      }
    },
    hide : function() {
      var e = this;
      e.ss = false;
      if (e.k > 0 && e.il > -1) {
        fwall.form.clear('thumbnail');
        $(e.ii[e.il]).hide();
      }
      e.ud();
    },
    toggle : function() {
      var e = this;
      if (e.ss) {
        e.hide();
      } else {
        e.show();
      }
    },
    ud : function() {
      var e = this, n = 'thumbnail';
      if (e.ss) {
        if (e.il && e.ii.length) {
          fwall.form.val(n, $(e.ii[e.il]).find('img').attr('src'));
        }
      } else {
        fwall.form.clear(n);
      }
    }
  },
  hdc : function() {
    $('div#idfhd').html('');
    $('ul#idfwll').show();
  },

  getIndImg : function() {
    return '<img src="' + oParams.sJsHome + 'module/feed/static/image/ajax-loader.gif' + '" />';
  },
  showInd : function() {
    $('div#idfhd').html(this.getIndImg());
  },

  blogpublic : function(f,sType) {
    for(i=1;i<=4;i++)
    {
        $('#'+i).removeClass('label_flow_menu_active');
    }
    $('#'+f).attr('class','label_flow_menu_active last');
    $('#js_category_content').html(this.getIndImg());
    $.ajaxCall('wall.displayCategories', 'sType='+sType);
  },
  
  form : {
    a : function() {
      return 'input#feed_'
    },
    b : function() {
      return [ 'title', 'subtitle', 'domain', 'base_url', 'url',
      'thumbnail', 'description', 'is_internal', 'internal_url',
      'resource_type', 'code', 'feed_content' ];
    },
    init : function(o) {
      var e = this, b = e.b();
      for ( var i in b) {
        c = b[i];
        if (c && o[c] != null && o[c] != undefined) {
          e.val(c, o[c]);
        }
      }
    },
    val : function(n, v) {
      var e = this;
      $(e.a() + n).val(v);
    },
    clear : function(n) {
      var e = this;
      $(e.a() + n).val('');
    },
    clearAll : function() {
      var e = this, a = e.b();
      for ( var i in a) {
        e.clear(a[i])
      }
    }
  },
  attcb : function(o) {

  },
  bind : function(res) {
    eval(res);
    var e = fwall;
    var bd = $('div#atabd');
    var bd1 = $('div#atabd1');
    if (o.responseCode == 'success') {
      bd1.html(e.pre());
      bd.css('display','none');
      o.subtitle = o.url;
      e.getLink = function() {
        return '<a href="' + o.url + '" target="_default"></a>';
      }
      e.getOrgLink = function() {
        return '<a href="' + o.url + '" target="_default"></a>';
      }
      e.title(o.title);
      e.description(o.description);
      e.form.init(o);
      if (o.images) {
        e.reads(o.images, o.baseUrl);
        window.setTimeout('fwall.thumb.init()', 1400);
      }
    }
  },
  attach : function() {
    var e = this;
    var a = $('input#id_feed_link').val();
    fwall.clear();
    $('div#id_feed_result_image').html('');
    $bButtonSubmitActive = true;
    //http://www.w3schools.com/jsref/jsref_obj_regexp.asp
    a = a.replace(/\s+/gi, ' ').replace(/^\s+/, '').replace(/\s+$/, '');
    // this.showInd();
    if (this.isLink(a)) {
      $.post(fwall.ajaxUrl(), fwall.mat( {
        link : a
      }, 'feed.fetch'), e.bind);
    }
  },
  fetchItem : function(html) {
    $(html).prependTo('div#js_feed_content');
    fwall.clear();
  },
  video : {
    width : 398,
    height : 259
  },
  build : function(a, b, c) {
    var e = this;
    $(e.fetch[b](c)).appendTo($(a).parents('div.feed_wrap:first'));
  },
  pre : function() {
    return [
    '<div id="id_feed_result_holder">',
    '<div id="id_feed_result_image" style="float: left" class="feed_thumb"></div>',
    '<div id="id_feed_result_content" class="feed_content">',
    '<div id="id_feed_result_title" class="feed_title"></div>',
    '<div id="id_feed_result_subtitle" class="feed_subtitle"></div>',
    '<div id="id_feed_result_description" class="feed_description"></div>',
    '<div id="id_feed_thumb_manage">',
    '<a class="wall_prev" href="javascript:void(fwall.thumb.back())" style="color: #fff">.</a>',
    '<a class="wall_next" href="javascript:void(fwall.thumb.next())" style="color: #fff">.</a>',
    '<input type="checkbox" name="feed_no_thumb" id="id_feed_no_thumb" onclick="fwall.thumb.toggle()" />',
    '<label for="feed_no_thumb">no thumbnail</label></div>',
    '</div>', '</div>', '<div class="clear"></div>' ].join('');
  },
  cf : function(a) {
    this.showInd();
    $.ajaxCall('feed.getForm', 'f=' + a);
  },
  post : function() {
    //this.showInd();
    $('div#atarea').hide();
    $(fwall.getIndImg()).appendTo('div#idfhd');
    $('form#js_feed_comment_form').get(0).submit();
  },
  focus: function(e,v){
    if($(e).val().replace(/(\s+$)|(^\s+)/gi,'') == v){
      $(e).val('');
    }
  },
  blur: function(e,v){
    if($(e).val().replace(/(\s+$)|(^\s+)/gi,'') == ''){
      $(e).val(v);
    }
  }
};

function submitWall(form) {
  $('input#id_wall_status').val($('div#id_div_status').html());
  return true;
  $(this).ajaxCall('wall.add');
}

